package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.GpsPosTraceLogic;
import cn.topevery.garden.entity.dto.GpsPosTraceDTO;
import cn.topevery.garden.entity.po.GpsPosTrace;
import cn.topevery.garden.entity.query.GpsPosTraceQuery;
import cn.topevery.garden.entity.vo.GpsPosConvertPointsVO;
import cn.topevery.garden.entity.vo.GpsPosTraceVO;
import cn.topevery.garden.wapi.utils.CoordinateTransHelper;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.dto.PointDto;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * GPS详情
 *
 * @author code-generate
 * @date 2021-09-13
 */
@Api(tags = "GPS详情")
@RestController
@RequestMapping("/gpsPosTrace")
public class GpsPosTraceController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    GpsPosTraceLogic gpsPosTraceLogic;

    @ApiOperation(value = "获取GPS详情页列表")
    @PostMapping("/convertPoints")
    public JsonResult<List<PointDto>> convertPoints(@RequestBody GpsPosConvertPointsVO vo) {
        List<PointDto> result=new ArrayList<PointDto>();
        List<PointDto> wgs84=new ArrayList<PointDto>();
        List<PointDto> gcj02=new ArrayList<PointDto>();
        switch (vo.getSourceType()){
            case "wgs84":
                wgs84=vo.getPoints();
                break;
            case "bd09":
                for (PointDto f:
                     vo.getPoints()) {
                    wgs84.add(CoordinateTransHelper.bd09WGS84(f.getLng(),f.getLat()));
                }
                break;
            default:
                break;
        }
        switch (vo.getTargetType()){
            case "wgs84":
                result=wgs84;
                break;
            case "gcj02":
                for (PointDto f:
                        wgs84) {
                    gcj02.add(CoordinateTransHelper.gcjEncrypt(f.getLng(),f.getLat()));
                }
                result=gcj02;
                break;
        }
        return JsonResult.ok(result);
    }

    /**
     * 获取GPS详情分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取GPS详情页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<GpsPosTraceVO>> listPage(@RequestBody GpsPosTraceQuery query) {
        return JsonResult.ok(gpsPosTraceLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 GPS详情信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody GpsPosTraceDTO dto) {
        gpsPosTraceLogic.saveOrUpdate(getUser().userId,mapper.map(dto, GpsPosTrace.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        gpsPosTraceLogic.deleteBatchById(getUser().userId,ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出GPS详情信息")
    @GetMapping("/export")
    public void export(GpsPosTraceQuery query, HttpServletResponse response) {
        List<GpsPosTraceVO> list = gpsPosTraceLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "GPS详情", "sheet1", GpsPosTraceVO.class, "GPS详情", response);
    }

}
